import { acceptHMRUpdate, defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => {
    return {
      userId: 0,
      token: '',
      userName: '',
      name: '',
    }
  },
  getters: {
    logged(state) {
      return state.userId > 0
    },
  },
  actions: {
    login(userId: number, token: string, userName: string, name: string) {
      this.userId = userId
      this.token = token
      this.userName = userName
      this.name = name
    },
    logout() {
      this.userId = 0
      this.token = ''
      this.userName = ''
      this.name = ''
    },
  },
  persist: true,
})

if (import.meta.hot)
  import.meta.hot.accept(acceptHMRUpdate(useUserStore as any, import.meta.hot))
